﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;

namespace DarkNeuron.Services.Input
{
// #if xbox
	public class ButtonGamePad : Button
	{
		protected GamePadButtons key;
		protected PlayerIndex playerIndex;


		public ButtonGamePad(string name, GamePadButtons key, PlayerIndex playerIndex)
			: base(name, ButtonDevice.GamePad)
		{
			Console.WriteLine("ButtonGamePad|Information:> New ButtonGamePad \"{0}\" is being initialized...", Name);

			bool pass = true;
			this.key = key;
			this.playerIndex = playerIndex;

			pass = ButtonManager.AddButton(this);

			if (pass) Console.WriteLine("ButtonGamePad|Information:> ButtonGamePad \"{0}\" is successfully initialized.", Name);
			else Console.WriteLine("ButtonGamePad|Information:> ButtonGamePad\"{0}\" is NOT successfully initialized.", Name);
		}


		public override void Update(GameTime gameTime)
		{
			if (Enabled)
			{
				// Sets current button states
				previousButtonState = currentButtonState;
				currentButtonState = CheckState();
			}
			
			
			base.Update(gameTime);
		}
		private ButtonState CheckState()
		{
			// Finds state of correct button on game pad
			switch (key)
			{
				case GamePadButtons.A:
					return (GameInput.GamePads[(int)playerIndex].Buttons.A == Microsoft.Xna.Framework.Input.ButtonState.Pressed ? ButtonState.Down : ButtonState.Up);
				case GamePadButtons.B:
					return (GameInput.GamePads[(int)playerIndex].Buttons.B == Microsoft.Xna.Framework.Input.ButtonState.Pressed ? ButtonState.Down : ButtonState.Up);
				case GamePadButtons.X:
					return (GameInput.GamePads[(int)playerIndex].Buttons.X == Microsoft.Xna.Framework.Input.ButtonState.Pressed ? ButtonState.Down : ButtonState.Up);
				case GamePadButtons.Y:
					return (GameInput.GamePads[(int)playerIndex].Buttons.Y == Microsoft.Xna.Framework.Input.ButtonState.Pressed ? ButtonState.Down : ButtonState.Up);
				case GamePadButtons.DPadLeft:
					return (GameInput.GamePads[(int)playerIndex].DPad.Left == Microsoft.Xna.Framework.Input.ButtonState.Pressed ? ButtonState.Down : ButtonState.Up);
				case GamePadButtons.DPadRight:
					return (GameInput.GamePads[(int)playerIndex].DPad.Right == Microsoft.Xna.Framework.Input.ButtonState.Pressed ? ButtonState.Down : ButtonState.Up);
				case GamePadButtons.DPadUp:
					return (GameInput.GamePads[(int)playerIndex].DPad.Up == Microsoft.Xna.Framework.Input.ButtonState.Pressed ? ButtonState.Down : ButtonState.Up);
				case GamePadButtons.DPadDown:
					return (GameInput.GamePads[(int)playerIndex].DPad.Down == Microsoft.Xna.Framework.Input.ButtonState.Pressed ? ButtonState.Down : ButtonState.Up);
				case GamePadButtons.LeftButton:
					return (GameInput.GamePads[(int)playerIndex].Buttons.LeftShoulder == Microsoft.Xna.Framework.Input.ButtonState.Pressed ? ButtonState.Down : ButtonState.Up);
				case GamePadButtons.RightButton:
					return (GameInput.GamePads[(int)playerIndex].Buttons.RightShoulder == Microsoft.Xna.Framework.Input.ButtonState.Pressed ? ButtonState.Down : ButtonState.Up);
				case GamePadButtons.Back:
					return (GameInput.GamePads[(int)playerIndex].Buttons.Back == Microsoft.Xna.Framework.Input.ButtonState.Pressed ? ButtonState.Down : ButtonState.Up);
				case GamePadButtons.Start:
					return (GameInput.GamePads[(int)playerIndex].Buttons.Start == Microsoft.Xna.Framework.Input.ButtonState.Pressed ? ButtonState.Down : ButtonState.Up);
				case GamePadButtons.XBoxButton:
					return (GameInput.GamePads[(int)playerIndex].Buttons.BigButton == Microsoft.Xna.Framework.Input.ButtonState.Pressed ? ButtonState.Down : ButtonState.Up);
				case GamePadButtons.LeftStick:
					return (GameInput.GamePads[(int)playerIndex].Buttons.LeftStick == Microsoft.Xna.Framework.Input.ButtonState.Pressed ? ButtonState.Down : ButtonState.Up);
				case GamePadButtons.RightStick:
					return (GameInput.GamePads[(int)playerIndex].Buttons.RightStick == Microsoft.Xna.Framework.Input.ButtonState.Pressed ? ButtonState.Down : ButtonState.Up);
				default:
					Console.WriteLine("ButtonGamePad|Critical:> key typeof(GamePadButtons) provided is not valid! Button Name: {0}", Name);
					return ButtonState.Up;
			}
		}
	}
// #endif
}
